package com.fp.m7.shared.validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fp.m7.shared.ValidationConstants;
import com.fp.m7.shared.ValidationError;
import com.fp.m7.shared.ValidationResult;
import com.google.inject.Inject;

@Component
public class RegistrationValidator {

    private NewPasswordsValidator newPasswordsValidator;

    public RegistrationValidator() {
    }

    /**
     * GIN clientside autowiring.
     */
    @Inject
    public RegistrationValidator(NewPasswordsValidator newPasswordsValidator) {
        this.newPasswordsValidator = newPasswordsValidator;
    }

    public ValidationResult validate(String username, String nickname, String password, String passwordRepeated) {
        ValidationResult result = new ValidationResult();

        if (username == null || !username.matches(ValidationConstants.USERNAME_REGEX)) {
            result.add(new ValidationError(ValidationConstants.Fields.USERNAME, ValidationConstants.Messages.REGEX_FAIL));
        }
        if (nickname == null || !nickname.matches(ValidationConstants.NICKNAME_REGEX)) {
            result.add(new ValidationError(ValidationConstants.Fields.NICKNAME, ValidationConstants.Messages.REGEX_FAIL));
        }

        result.addAll(newPasswordsValidator.validate(password, passwordRepeated));

        return result;
    }

    /**
     * Spring serverside autowiring.
     */
    @Autowired
    public void setNewPasswordsValidator(NewPasswordsValidator newPasswordsValidator) {
        this.newPasswordsValidator = newPasswordsValidator;
    }

}
